package com.bestcem.xm.deliver.mq.send.biz.impl;

import cn.hutool.json.JSONUtil;
import com.bestcem.xm.common.core.config.mq.XmMessageProducer;
import com.bestcem.xm.common.core.constant.StreamMqBinderConstants;
import com.bestcem.xm.deliver.mq.dto.*;
import com.bestcem.xm.deliver.mq.send.biz.DeliverMessageSendService;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class DeliverMessageSendServiceImpl implements DeliverMessageSendService {

    private final XmMessageProducer messageProducer;

    public DeliverMessageSendServiceImpl(XmMessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }
    //private XmMessageProducer messageProducer;
    //
    //@Autowired
    //public void setMessageProducer(XmMessageProducer messageProducer) {
    //    this.messageProducer = messageProducer;
    //}

    @Override
    public void wechatmpDeliverSend(WechatmpDeliverMessageDto dto, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.WECHATMP_DELIVER_OUT, dto, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[deliver]公众号投放发送延迟消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void wechatmpDeliverSend(WechatmpDeliverMessageDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.WECHATMP_DELIVER_OUT, dto);
        } catch (JsonProcessingException e) {
            log.error("[deliver]公众号投放发送消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void emailDeliverSend(EmailDeliverMessageDto dto, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.EMAIL_DELIVER_OUT, dto, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[deliver]邮件投放发送延迟消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void emailDeliverSend(EmailDeliverMessageDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.EMAIL_DELIVER_OUT, dto);
        } catch (JsonProcessingException e) {
            log.error("[deliver]邮件投放发送消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void smsDeliverSend(SmsDeliverMessageDto dto, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.SMS_DELIVER_OUT, dto, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[deliver]邮件投放发送延迟消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void smsDeliverSend(SmsDeliverMessageDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.SMS_DELIVER_OUT, dto);
        } catch (JsonProcessingException e) {
            log.error("[deliver]邮件投放发送消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void saveCustomDeliverLinkSend(CustomDeliverLinkMessageDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.SAVE_CUSTOM_DELIVER_LINK_OUT, dto);
        } catch (JsonProcessingException e) {
            log.error("[deliver]自定义链接保存发送消息失败:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void projectUpdated(StatusDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.PROJECT_UPDATED, dto);
        } catch (JsonProcessingException e) {
            log.error("[qdes]项目变更消息:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void surveyCreateColumns(CreateColumnsDto dto) {
        try {
            messageProducer.send(StreamMqBinderConstants.SURVEY_CREATE_COLUMNS, dto);
        } catch (JsonProcessingException e) {
            log.error("[survey]答卷数据创建列:{}", JSONUtil.toJsonStr(dto), e);
        }
    }

    @Override
    public void openplatformExtParamChangedSend(Map<String, Object> data) {
        try {
            messageProducer.send(StreamMqBinderConstants.OPENPLATFORM_EXTPARAM_CHANGED, data);
        } catch (JsonProcessingException e) {
            log.error("[survey]openplatform_extparam_changed发送消息失败:{}", JSONUtil.toJsonStr(data), e);
        }
    }
}
